// pages/message/message.js
import {request} from "../../request/request.js";
import moment from "../../utils/moment";
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户Id
    userId: 0,
    parentMessageId: 0,
    position: false,
    show: false,
    message: '',
    listMessageVo: [],
    pageNo: 1,
    pageSize: 5,
    total: 0,
    isMore: false,
    placeholder: '请输入留言：'
  },
  onInput(event) {
    this.setData({
      message: event.detail
    })
  },
  onOpen() {
    if(this.data.userId === 0) {
      Notify({ type: 'danger', message: '请先登录！' });
      return;
    }
    this.setData({ show: true });
  },
  onClose() {
    this.setData({
      message: '',
      placeholder: '请输入留言：',
      parentMessageId: 0
    })
    this.setData({ show: false });
  },
  async onConfirm() {
    let message = this.data.message;
    let parentMessageId = this.data.parentMessageId;
    let userId = this.data.userId;
    if(message === '') {
      Notify({ type: 'danger', message: '留言不能为空！' });
      return;
    }
    const res = await request({
      url: '/message/insertMessage',
      method: 'post',
      data: {
        parentMessageId,
        content: message,
        userId
      }
    })
    if(res.code === 2000) {
      Notify({ type: 'success', message: '发表留言成功！' });
      this.listMessageVo();
      this.onClose();
    }
  },
  //删除留言
  async delete(e) {
    let messageId = e.target.dataset.id;
    console.log(messageId);
    const res = await request({
      url: '/message/deleteMessage',
      method: 'delete',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        messageId
      }
    })
    if(res.code === 2000) {
      Notify({ type: 'success', message: '删除留言成功！' });
      this.listMessageVo();
    }
  },
  //回复留言
  reply(e) {
    let index = e.target.dataset.index;
    console.log(index);
    let messageId = this.data.listMessageVo[index].id;
    let nickname = this.data.listMessageVo[index].nickname;
    console.log(messageId);
    console.log(nickname);
    if(this.data.userId === 0) {
      Notify({ type: 'danger', message: '请先登录！' });
      return;
    }
    this.setData({
      placeholder: '回复：'+nickname,
      parentMessageId: messageId,
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if(userInfo !== null && userInfo !== '') {
      this.setData({
        userId: userInfo.userId,
        position: userInfo.position
      })
    }
    //获取数据
    this.listMessageVo();
  },
  //获取留言信息
  async listMessageVo() {
    const res = await request({
      url: '/message/listMessageVo',
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize
      }
    })
    console.log(res);
    if(res.code === 2000) {
      const listMessageVo = res.data.messageVoList;
      const total = res.data.total;
      listMessageVo.forEach(element => {
        element.gmtCreate = moment(element.gmtCreate).format("MMMM Do YYYY, h:mm:ss a");
        if(element.childrenMessage.lenght !== 0) {
          element.childrenMessage.forEach(children => {
            children.gmtCreate = moment(children.gmtCreate).format("MMMM Do YYYY, h:mm:ss a");
          })
        }
      });
      this.setData({
        listMessageVo,
        total
      })

    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    if(pageNo * this.data.pageSize >= this.data.total) {
      console.log("没有数据了")
      this.setData({
        isMore: true
      })
      return;
    }
    this.setData({
      pageNo: pageNo + 1
    })
    console.log("触底了");
    this.listMessageVo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})